package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
   private SupplierDao supplierDao;

    //第二个接口分页查询供应商
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
       Map<String, Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
      List<Supplier>  supplierList=supplierDao.list(offSet,rows,supplierName);
       map.put("total", supplierList.size());
       map.put("rows", supplierList);

        return map;
    }

    //第三个接口 供应商添加或修改
    @Override
    public Boolean saveOrUpdate(Supplier supplier) {
        //获取供应商id
        Integer supplierId = supplier.getSupplierId();
        try {
            if(supplierId==null){
                //新增
                supplierDao.save(supplier);
                return true;

            }else{
                //修改
                supplierDao.update(supplier);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }

    //第四个接口 批量删除供应商
    @Override
    public Boolean delete(String ids) {

        try {
            String[] split = ids.split(",");
            supplierDao.delete(split);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }
}
